<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/9/5
 * Time: 11:05
 */

/**
 * Created by wanglu on 2017/9/5.
 */
class Project_service extends MY_Service
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('project_model');
        $this->load->model('lang_model');
        $this->load->model('key_model');
        $this->load->model('project_user_model');
        $this->load->model('lang_user_model');
        $this->load->model('project_content_temp_model');
    }

    /**
     * 创建项目
     * @param $pname
     * @param $remark
     * @param $owner_id
     * @param array $langs
     * @param array $keys
     * @return bool
     * @throws AppException
     */
    public function createProject($pname, $remark, $owner_id, $langs = array(), $keys = array()) {

        $p = $this->project_model->getRecord(array('name' => $pname));

        if (!empty($p)) {
            throw  new AppException(ErrorCode::FAIL, " project name is exits , create project fail!");
        }

        $project = array(
            'name'=> $pname,
            'remark'=> $remark,
            'owner_id'=> $owner_id,
            'createtime'=> time(),
            'updatetime'=> time()
        );


        $projectId = $this->project_model->createRecordReturnId($project);
        if ($projectId) {

            $project_user = array(
                'pid'=> $projectId,
                'uid'=> $owner_id,
                'role'=> AppConstants::PROJECT_ROLE_OWNER,
                'createtime'=> time()
            );

            $this->project_user_model->createRecord($project_user);


            foreach ($langs as $lang) {
                $langData = array(
                    'pid'=> $projectId,
                    'name'=> $lang['name'],
                    'shorthand' => $lang['shorthand'],
                    'remark' => $lang['remark'],
                    'createtime' => time()
                );

                $this->lang_model->createRecord($langData);
            }

            foreach ($keys as $key) {
                $keyData = array(
                    'pid'=> $projectId,
                    'pkey'=> $key['pkey'],
                    'limit_length'=> $key['limit_length'],
                    'remark'=> $key['remark'],
                    'createtime' => time()
                );
                $this->key_model->createRecord($keyData);
            }
        } else {
            throw  new AppException(ErrorCode::FAIL, " create project fail!");
        }

        return true;
    }

    public function delete_key($pid, $pkey) {
        //临时里面删除
        $this->project_content_temp_model->deleteRecord(array('pid'=> $pid, 'pkey'=> $pkey));
        return $this->key_model->deleteRecord(array('pid'=> $pid, 'pkey'=> $pkey));
    }


    public function get_project_keys($pid) {
        return $this->key_model->get_pkeys($pid);
    }
    /**
     * 获取用户的可以管理查看的项目
     * @param $user
     * @return mixed
     */
    public function get_user_projects($user) {
        return $this->project_user_model->get_user_projects($user['id']);
    }


    /**
     * 获取用户创建的项目
     * @param $user
     *
     * @return mixed
     */
    public function get_user_own_projects($user) {
       return $this->project_user_model->get_projects_by_owner_id($user['id']);
    }

    /**
     * 获取用户的语言权限
     * @param $pid
     * @param $user_id
     * @return mixed
     */
    public function get_user_langs($pid, $user_id) {
        return $this->lang_user_model->get_user_langs($pid, $user_id);
    }

    /**
     * 获取项目的成员
     * @param $project
     * @return mixed
     */
    public function get_project_users($pid) {
        return $this->project_user_model->get_project_users($pid);
    }

    public function get_project_by_id($id) {
        return $this->project_model->getRecord(array('id' => $id));
    }

    public function get_project_user_relation($pid, $user_id) {
        return $this->project_user_model->getRecord(array('pid' => $pid, 'uid'=> $user_id));
    }

    public function search_not_in_project_users($username, $pid) {
        return $this->project_user_model->search_not_in_project_users($username, $pid);
    }

    /**
     * 添加项目成员
     * @param $user_id
     * @param $project_id
     * @param $role
     * @param $lang_permissions
     * @return bool
     * @throws AppException
     */
    public function add_project_user($user_id, $project_id, $role, $lang_permissions) {

        $p_u = $this->project_user_model->getRecord(array('pid' => $project_id, 'uid'=> $user_id));

        if (!empty($p_u)) {
            throw  new AppException(ErrorCode::FAIL, " the project is exits this user , add user fail!");
        }

        $project_user = array(
            'pid'=> $project_id,
            'uid'=> $user_id,
            'role'=> $role,
            'createtime'=> time()
        );

        //添加项目用户
        $this->project_user_model->createRecord($project_user);

        //如果是管理员  移除 语言权限
        if ($role == AppConstants::PROJECT_ROLE_ADMIN) {
            $this->lang_user_model->deleteRecord(array('pid'=> $project_id, 'uid'=> $user_id));
        } else {

          foreach ($lang_permissions as $lang_permission) {
              $lang_data = array(
                  'pid'=> $project_id,
                  'uid'=> $user_id,
                  'lang_id'=>$lang_permission['lang_id'],
                  'permission'=> $lang_permission['permission'],
                  'createtime'=> time()
              );

              //创建语言权限
               $this->lang_user_model->createRecord($lang_data);
          }

        }
        return true;
    }


    public function update_project_user($user_id, $project_id, $role, $lang_permissions) {

        $p_u = $this->project_user_model->getRecord(array('pid' => $project_id, 'uid'=> $user_id));

        if (empty($p_u)) {
            throw  new AppException(ErrorCode::FAIL, " the project is not exits this user , update  user fail!");
        }

        // 更变了角色
        if ($p_u['role'] != $role) {
            $this->project_user_model->updateRecord(array('role'=> $role), array('pid'=> $project_id, 'uid'=> $user_id));
        }

        //删除所有语言权限 重新更新
        $this->lang_user_model->deleteRecord(array('pid'=> $project_id, 'uid'=> $user_id));

        if ($role == AppConstants::PROJECT_ROLE_INTERPRETER) {
            foreach ($lang_permissions as $lang_permission) {
                $lang_data = array(
                    'pid' => $project_id,
                    'uid' => $user_id,
                    'lang_id' => $lang_permission['lang_id'],
                    'permission' => $lang_permission['permission'],
                    'createtime' => time()
                );
                //创建语言权限
                $this->lang_user_model->createRecord($lang_data);
            }
        }

        return true;
    }

    /**
     * 移除项目团队成员
     * @param $pid
     * @param $uid
     * @return mixed
     */
    public function remove_project_user($pid, $uid) {
        $this->lang_user_model->deleteRecord(array('pid'=> $pid, 'uid'=> $uid));
        return $this->project_user_model->deleteRecord(array('pid'=> $pid, 'uid'=> $uid));
    }

    public function delete_project_by_id($pid) {
        return $this->project_model->updateRecord(array('is_deleted'=> 1), array('id'=> $pid));
    }

    public function update_project($pid, $name, $remark) {


        $project_extis = $this->project_model->getRecord(array('id' => $pid, 'is_deleted'=> 0));
        
        if (empty($project_extis)) {
            throw  new AppException(ErrorCode::FAIL, " project  is not exits!");
        }

        //修改了缩写
        if ($project_extis['name'] != $name) {

            //查询是否有同名存在
            $name_project_extis = $this->project_model->getRecord(array('name' => $name, 'is_deleted'=> 0));
            if (!empty($name_project_extis)) {
                throw  new AppException(ErrorCode::FAIL, " project name  is exits, add fail, please change project name!");
            }
        }

        $project_data = array(
            'name'=> $name,
            'remark' => $remark
        );

        return $this->project_model->updateRecord($project_data,  array('id'=> $pid));
    }
}
